package cn.microven.mindmap.components.control;

import cn.microven.mindmap.components.part.MapPart;
import cn.microven.mindmap.components.part.Part;
import cn.microven.mindmap.oprations.CreateStageOperation;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;


/**
 * 节点右键菜单
 */
public class NodeContextMenu extends ContextMenu {

    /**
     * 单例
     */
    //private static NodeContextMenu INSTANCE = null;
    protected MapNodeLabel text;

    protected MapNodeTextArea textArea;

    String httplink;

    protected Part boundPart;



    public NodeContextMenu(MapPart node) {

        //textArea=node.getRoot().getTextArea();
        //text=node.getRoot().gettext();



        Menu addMenuItem = new Menu("添加");
        MenuItem addchildMenuItem = new MenuItem("孩子");
        MenuItem addbroMenuItem = new MenuItem("同级");
        addMenuItem.getItems().addAll(addbroMenuItem,addchildMenuItem);
        /*if(boundPart == null)
            System.out.println("你是空的");*/
        addchildMenuItem.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                boundPart.addLeaf();
            }
        });

        addbroMenuItem.setOnAction(actionEvent -> {
            if (node.getParentPart()!=null) {
                node.getParentPart().addLeaf();
            }
        });

        MenuItem deleteMenuItem = new MenuItem("删除");

        deleteMenuItem.setOnAction(actionEvent -> {
            if (node.getParentPart()!=null) {
                    System.out.println("删除节点");
                    node.getParentPart().removeLeaf(node);
                }
        });

        Menu changeMenu = new Menu("修改");
        MenuItem changeFontMeunitem = new MenuItem("字体");
        changeMenu.getItems().addAll(changeFontMeunitem);

        changeFontMeunitem.setOnAction(event->{
            Platform.runLater(() -> {

                CreateStageOperation cso = new CreateStageOperation();
                try {
                    cso.start(new Stage());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });


        });



        Menu insertMenuItem = new Menu("插入");
        //MenuItem addtextMenuItem = new MenuItem("文本");
        MenuItem addlinkMenuItem = new MenuItem("链接");
        //insertMenuItem.getItems().addAll(addtextMenuItem,addlinkMenuItem);

        //this.text.textProperty().bindBidirectional(textArea.textProperty());

        /*addtextMenuItem.setOnAction(actionEvent -> {

            this.showTxetArea();

        });*/


        addlinkMenuItem.setOnAction(actionEvent -> {
            this.showTxetArea();
            Hyperlink link = new Hyperlink();
            System.out.println(text.getText());
            httplink=text.getText();
            link.setText(httplink);
            System.out.println(httplink);
            node.getChildren().addAll(link);

            link.setOnAction(e -> {
                hideTxetArea();
                WebView browser = new WebView();
                WebEngine webEngine = browser.getEngine();

                System.out.println(httplink);
                webEngine.load(httplink);

            });
        });

        Menu hideMenu = new Menu("隐藏");
        MenuItem hidetextMenuItem = new MenuItem("文本框");
        MenuItem hidelnodeMenuItem = new MenuItem("子结点");
        hideMenu.getItems().addAll(hidelnodeMenuItem);

        hidetextMenuItem.setOnAction(actionEvent -> {
             this.hideTxetArea();
        });

        hidelnodeMenuItem.setOnAction(actionEvent -> {
            node.toggleChildrenVisible();
        });



        getItems().addAll(addMenuItem,deleteMenuItem,changeMenu,hideMenu);
        /*getItems().add(deleteMenuItem);
        getItems().add(insertMenuItem);*/


        this.boundPart = node;
        node.getRoot().addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.getButton() == MouseButton.SECONDARY) {
                    NodeContextMenu.this.show(node.getRoot(), event.getScreenX(), event.getScreenY());
                } else {
//                    System.out.println("No right click");

                }
            }
        });


    }
    public void showTxetArea(){
        textArea.setDisable(false);
        textArea.setVisible(true);
        textArea.requestFocus();
    }

    public void hideTxetArea(){
        textArea.setDisable(true);
        textArea.setVisible(false);

    }

    public boolean isHasShowTextArea(){
        if (text.isDisable()==false && text.isVisible()==true)
            return true;
        return false;
    }


    /**
     * 获取实例
     * @return NodeContextMenu
     */
    /*public static NodeContextMenu getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new NodeContextMenu();
        }
        return INSTANCE;
    }*/



    /*public void addRightMenu(Part node) {
        node.setOnMouseClicked(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent event)

            {
                if(event.isSecondaryButtonDown() && event.getClickCount()==1)
                    NodeContextMenu.getInstance().show(node, Side.BOTTOM,0,0);
            }
        });
    }*/

}
